package com.lc.demo.service.impl;


import com.lc.demo.bean.Classgrade;
import com.lc.demo.bean.Student;
import com.lc.demo.mapper.ClassgradeMapper;
import com.lc.demo.mapper.StudentMapper;
import com.lc.demo.service.ClassgradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClassgradeServiceImpl implements ClassgradeService {


    @Autowired
    private ClassgradeMapper classgradeMapper;

    @Override
    public List<Classgrade> getClassGradeList(Classgrade classgrade) {
        return classgradeMapper.select(classgrade);
    }

    @Override
    public void updateClassGrade(Classgrade classgrade) {
        classgradeMapper.updateByPrimaryKeySelective(classgrade);
    }

    @Override
    public void addClassGrade(Classgrade classgrade) {
        classgradeMapper.insertSelective(classgrade);
    }

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public void delClassGrade(Classgrade classgrade) {
        //查询出该班级下的所有学生
        List<Student> studentList = classgradeMapper.getStudentListByClassGrade(classgrade.getId());
        for (Student student : studentList) {
            //删除签到表  删除请假表
            classgradeMapper.deleteSignByStudentId(student.getUsername());
            classgradeMapper.deleteLeaveByStudentId(student.getUsername());
            //删除班级下的所有学生
            studentMapper.delStudentByUsername(student.getUsername());
        }
        //删除课程 班级关系表
        classgradeMapper.deleteGradeCourseByClassgradeid(classgrade.getId());
        //删除课程
        classgradeMapper.deleteByPrimaryKey(classgrade.getId());
    }
}
